/**
 * 
 */
package de.midnightdance.admin.clan;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import de.cbf.util.ui.editentities.controller.ControllerEditEntities;
import de.cbf.util.ui.editentities.interfaces.ICompositeEditEntities;
import de.cbf.util.ui.editentities.interfaces.IEditableEntity;
import de.midnightdance.admin.util.WebServiceProvider;
import de.midnightdance.admin.ws.WebServiceAdminRequiem;
import de.midnightdance.admin.ws.entities.Clan;
import de.midnightdance.admin.ws.entities.RequiemUser;
import de.midnightdance.admin.ws.entities.arrays.ClanArray;

/**
 * @author Stefan Alter
 *
 */
public class PresenterEditClans extends ControllerEditEntities {

	private WebServiceAdminRequiem port;
	//
	private RequiemUser user;
	
	private PCLforView pcl;
	private ListenerSaveNewEntities listenerSaveNewEntities;
	/**
	 * 
	 */
	public PresenterEditClans() {
		super();
		port = WebServiceProvider.getWebService();
		//
		setControllerEditEntity(new PresenterEditClan());
		//
		listenerSaveNewEntities = new ListenerSaveNewEntities();
		registerSaveEditListener(listenerSaveNewEntities);
	}
	
	public void initData() {
		ClanArray array;		
		System.out.println("Loading Clans...");
		array = port.getClane(user);
		List<Clan> entites = array.getItem();
		setEntities(entites.iterator());				
	}

	public void saveNewEntities() {
		System.out.println("Now saving new Clans...");
		for (IEditableEntity entity : entities) {
			if (entity instanceof Clan) {
				Clan typedEntity = (Clan) entity;
				saveEntity(typedEntity);
			}
		}		
	}
	
	private void saveEntity(Clan entity){
		int result = port.addClan(entity, user);
		if(result != 0){
			System.err.println("Adding of Clan "+entity+" failed");
		}
	}
	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.controller.ControllerEditEntities#checkEntity(de.cbf.util.ui.editentities.interfaces.IEditableEntity)
	 */
	@Override
	protected boolean checkEntity(IEditableEntity value)
			throws IllegalArgumentException {
		return (value instanceof Clan);
	}

	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.controller.ControllerEditEntities#checkView(de.cbf.util.ui.editentities.interfaces.ICompositeEditEntities)
	 */
	@Override
	protected boolean checkView(ICompositeEditEntities value)
			throws IllegalArgumentException {
		return (value instanceof CompositeEditClans);
	}

	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.controller.ControllerEditEntities#getEntityChangedListener()
	 */
	@Override
	protected PropertyChangeListener getEntityChangedListener() {
		return pcl;
	}

	/* (non-Javadoc)
	 * @see de.cbf.util.ui.editentities.controller.ControllerEditEntities#getNewEntity()
	 */
	@Override
	protected IEditableEntity getNewEntity() {
		return new Clan();
	}
	/**
	 * @return the user
	 */
	public RequiemUser getUser() {
		return user;
	}

	/**
	 * @param user the user to set
	 */
	public void setUser(RequiemUser user) {
		this.user = user;
	}
	
	private class PCLforView implements PropertyChangeListener{
		@Override
		public void propertyChange(PropertyChangeEvent evt) {
			view.setEntities(entities.iterator());			
		}		
	}

	private class ListenerSaveNewEntities implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			saveNewEntities();			
		}		
	}

}
